package com.lyhengtongwl.zqsnews.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.entity.MyaddrEntity;
import com.lyhengtongwl.zqsnews.ui.activity.AddaddrActivity;

import java.util.List;

/**
 * Created by Steven on 2019/1/26.
 */
public class SwipeMenuListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<MyaddrEntity> data;
    private Activity activity;

    public SwipeMenuListAdapter(Activity activity, LayoutInflater inflater, List<MyaddrEntity> data) {
        this.activity = activity;
        this.inflater = inflater;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final Holder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.add_address_item, null);
            holder = new Holder();
            holder.tv_name = view.findViewById(R.id.tv_name);
            holder.tv_mobile = view.findViewById(R.id.tv_mobile);
            holder.tv_def_addr = view.findViewById(R.id.tv_moren);
            holder.tv_add = view.findViewById(R.id.tv_addr);
            holder.iv_edit = view.findViewById(R.id.iv_edit);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.tv_name.setText(data.get(position).getReceiver());
        StringBuilder sb = new StringBuilder(data.get(position).getMobile());
        sb.replace(3, 7, "****");
        holder.tv_mobile.setText(sb.toString());
        if ("1".equals(data.get(position).getStatus())) {//默认地址
            holder.tv_def_addr.setVisibility(View.VISIBLE);
        } else {
            holder.tv_def_addr.setVisibility(View.GONE);
        }
        holder.tv_add.setText(data.get(position).getAddress());
        holder.iv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.startActivityForResult(new Intent(activity, AddaddrActivity.class)
                        .putExtra("addressId", data.get(position).getAddressId()), 2);
            }
        });
        return view;
    }

    private class Holder {
        TextView tv_name, tv_mobile, tv_def_addr;
        TextView tv_add;
        ImageView iv_edit;
    }
}
